package model.entity;

import game_engine.GameEngine;
import game_engine.core.Entity;
import game_engine.core.EntityGroup;
import game_engine.core.Vector2f;
import game_engine.display.GEUtil;
import game_engine.display.Texture;
import game_engine.display.TextureLoader;
import game_engine.test.GameTest;

import java.awt.Color;
import java.awt.Rectangle;
import java.io.IOException;

public class Enemy extends Entity {

	private int life = 10;
	public static int FRAME_EXPLO = 0;
	public static int FRAME_NOMOVE = 1;

	private long actionTick = 1000;
	private long lastActionTick = 0;

	private static final Color transColor = new Color(0xbfdcbf);

	private EntityGroup bullets;

	public Enemy(String name) {
		super(name);
		Texture[][] frames = new Texture[2][];
		Texture[] explo = new Texture[13];
		GameEngine ge = GameEngine.getInstance();
		for (int i = 1; i <= 13; i++) {
			if (i < 10)
				explo[i - 1] = ge.getTexture("images/explo/explo_0" + i + ".png", transColor);
			else
				explo[i - 1] = ge.getTexture("images/explo/explo_" + i + ".png", transColor);
		}
		Texture[] noMove = new Texture[] { ge.getTexture("images/enemies/enemy1.png", transColor) };
		frames[1] = noMove;
		frames[0] = explo;

		int[][] framesCountList = new int[2][];
		int[] framesCount1 = new int[13];
		for (int i = 0; i < 13; i++) {
			framesCount1[i] = 30;
		}
		framesCountList[0] = framesCount1;

		int[] framesCount2 = new int[] { 0 };
		framesCountList[1] = framesCount2;

		boolean[] frameLoop = new boolean[2];
		frameLoop[0] = false;
		frameLoop[1] = false;
		currentFrame = 0;
		currentAction = FRAME_NOMOVE;

		boundingBox = new Rectangle(0, 0, 30, 30);

		this.setFrames(frames);
		this.setFrameCount(framesCountList);
		this.setFrameLoop(frameLoop);

		bullets = new EntityGroup();
	}

	public void render() {
		if (position != null) {
			GEUtil.drawImage(frames[currentAction][currentFrame], Math.round(position.x), Math.round(position.y), 30, 30, 255);
			if (currentAction == FRAME_EXPLO && currentFrame == 12)
				dead = true;
		}
	}

	public EntityGroup getBullets() {
		return bullets;
	}

	@Override
	public void move(long tick) {
		super.move(tick);
		// if(position.x > GEWindow.resolution.width) dead = true;
		// if(position.x < 0) dead = true;
		if (position.y > GameEngine.resolution.height)
			dead = true;
		// if(position.y < 0) dead = true;
	}

	@Override
	public void collided(Entity e) {
		if (e instanceof Bullet)
			life -= ((Bullet) e).getDamage();
		if (life <= 0) {
			unspawning = true;
			setCurrentAction(FRAME_EXPLO);
		}
	}

	@Override
	public void doAction(float tick) {
		if (lastActionTick > actionTick) {
			Bullet bullet = new Bullet(null);
			Vector2f position1 = new Vector2f(position.x, position.y - 5);
			bullet.setPosition(position1);
			bullet.setDirection(new Vector2f(0.0f, 1f));
			bullet.setSpeed(0.2f);
			// float[] rotations = new float[]{5f, -5f};
			// bullet.setRotations(rotations);
			// int[] rotationsTiming = new int[]{200, 200};
			// bullet.setRotationsTiming(rotationsTiming);
			// bullet.setRotationsLoop(true);
			bullet.setBox(new Rectangle(0, 0, 10, 10));
			bullets.addEntity(bullet);
			lastActionTick = 0;
		} else
			lastActionTick += tick;
	}

}
